{
LANG : PASCAL
PROB : mqueue
}

program MQUEUE;
const
  fi = 'mqueue.in';
  fo = 'mqueue.out';
  max = 25001;
type
  dur = record
          a, b : integer;
        end;
var
  c : array[1..max] of dur;
  n, res : longint;

procedure inp;
var
  i : longint;
begin
  read(n);
  for i := 1 to n do
    with c[i] do read(a, b);
end;

function gt(c1 : dur) : byte;
begin
  with c1 do
    if a <= b then gt := 0
    else gt := 1;
end;

function nhohon(c1, c2 : dur) : boolean;
var
  v1, v2 : longint;
begin
  v1 := gt(c1);
  v2 := gt(c2);
  if v1 < v2 then begin nhohon := true; exit; end else
  if v1 > v2 then begin nhohon := false; exit; end;
  if v1 = 0 then nhohon := c1.a < c2.a
  else nhohon := c1.b > c2.b;
end;

procedure swap(i, j : longint);
var
  temp : dur;
begin
  temp := c[i];
  c[i] := c[j];
  c[j] := temp;
end;

procedure sort(l, r : longint);
var
  i, j : longint;
  k : dur;
begin
  if l >= r then exit;
  i := l;
  j := r;
  k := c[l+random(r-l+1)];
  repeat
    while nhohon(c[i], k) do inc(i);
    while nhohon(k, c[j]) do dec(j);
    if i <= j then
    begin
      swap(i, j);
      inc(i); dec(j);
    end;
  until i > j;
  sort(i, r);
  sort(l, j);
end;

function max2(x, y : longint) : longint;
begin
  if x > y then max2 := x
  else max2 := y;
end;

procedure cal;
var
  i, ta, tb : longint;
begin
  ta := 0;
  tb := 0;
  for i := 1 to n do
   with c[i] do
    begin
      ta := ta + a;
      tb := max2(ta, tb) + b;
    end;
  res := tb;
end;

procedure process;
begin
  sort(1, n);
  cal;
end;

begin
  randomize;
  assign(input, fi); reset(input);
  assign(output, fo); rewrite(output);
  inp;
  process;
  writeln(res);
  close(output);
  close(input);
end.